﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace Scheduler.Model.MigrationsNpgsql
{
    /// <inheritdoc />
    public partial class InitDbNpgsql : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "modules",
                columns: table => new
                {
                    id = table.Column<long>(type: "bigint", nullable: false, comment: "泛型主键Tkey")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    name = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false, comment: "名称"),
                    link_url = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false, comment: "菜单链接地址"),
                    area = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true, comment: "区域名称"),
                    controller = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true, comment: "控制器名称"),
                    action = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true, comment: "Action名称"),
                    icon = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false, comment: "图标"),
                    code = table.Column<string>(type: "character varying(10)", maxLength: 10, nullable: true, comment: "菜单编号"),
                    order_sort = table.Column<int>(type: "integer", nullable: false, comment: "排序"),
                    description = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true, comment: "描述"),
                    is_menu = table.Column<bool>(type: "boolean", nullable: false, comment: "是否是右侧菜单"),
                    enabled = table.Column<bool>(type: "boolean", nullable: false, comment: "是否激活"),
                    is_deleted = table.Column<bool>(type: "boolean", nullable: false, comment: "逻辑删除"),
                    create_id = table.Column<long>(type: "bigint", nullable: false, comment: "创建者Id"),
                    create_by = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false, comment: "创建者"),
                    create_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "创建时间"),
                    modify_id = table.Column<long>(type: "bigint", nullable: true, comment: "修改Id"),
                    modify_by = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: true, comment: "更新者"),
                    modify_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "修改日期")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_modules", x => x.id);
                },
                comment: "接口API地址信息表");

            migrationBuilder.CreateTable(
                name: "permissions",
                columns: table => new
                {
                    id = table.Column<long>(type: "bigint", nullable: false, comment: "泛型主键Tkey")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    code = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false, comment: "菜单执行Action名"),
                    name = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false, comment: "菜单显示名（如用户页、编辑(按钮)、删除(按钮)）"),
                    is_button = table.Column<bool>(type: "boolean", nullable: false, comment: "是否是按钮"),
                    is_hide = table.Column<bool>(type: "boolean", nullable: false, comment: "是否是隐藏菜单"),
                    iskeep_alive = table.Column<bool>(type: "boolean", nullable: false, comment: "是否keepAlive"),
                    func = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true, comment: "按钮事件"),
                    order_sort = table.Column<int>(type: "integer", nullable: false, comment: "排序"),
                    icon = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false, comment: "菜单图标"),
                    description = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true, comment: "菜单描述"),
                    enabled = table.Column<bool>(type: "boolean", nullable: false, comment: "激活状态"),
                    is_deleted = table.Column<bool>(type: "boolean", nullable: false, comment: "逻辑删除"),
                    create_id = table.Column<long>(type: "bigint", nullable: false, comment: "创建者Id"),
                    create_by = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false, comment: "创建者"),
                    create_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "创建时间"),
                    modify_id = table.Column<long>(type: "bigint", nullable: true, comment: "修改Id"),
                    modify_by = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: true, comment: "更新者"),
                    modify_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "修改日期")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_permissions", x => x.id);
                },
                comment: "路由菜单表");

            migrationBuilder.CreateTable(
                name: "role_module_permissions",
                columns: table => new
                {
                    id = table.Column<long>(type: "bigint", nullable: false, comment: "泛型主键Tkey")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    role_id = table.Column<long>(type: "bigint", nullable: false),
                    module_id = table.Column<long>(type: "bigint", nullable: false),
                    permission_id = table.Column<long>(type: "bigint", nullable: false),
                    is_deleted = table.Column<bool>(type: "boolean", nullable: false, comment: "逻辑删除"),
                    create_id = table.Column<long>(type: "bigint", nullable: false, comment: "创建者Id"),
                    create_by = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false, comment: "创建者"),
                    create_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "创建时间"),
                    modify_id = table.Column<long>(type: "bigint", nullable: true, comment: "修改Id"),
                    modify_by = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: true, comment: "更新者"),
                    modify_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "修改日期")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_role_module_permissions", x => x.id);
                },
                comment: "按钮跟权限关联表");

            migrationBuilder.CreateTable(
                name: "roles",
                columns: table => new
                {
                    id = table.Column<long>(type: "bigint", nullable: false, comment: "泛型主键Tkey")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    name = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false, comment: "角色名"),
                    description = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false, comment: "描述"),
                    order_sort = table.Column<int>(type: "integer", nullable: false, comment: "排序"),
                    dids = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true, comment: "自定义权限的部门ids"),
                    authority_scope = table.Column<int>(type: "integer", nullable: false, comment: "权限范围 -1 无任何权限；1 自定义权限；2 本部门；3 本部门及以下；4 仅自己；9 全部；"),
                    enabled = table.Column<bool>(type: "boolean", nullable: false, comment: "是否激活"),
                    is_deleted = table.Column<bool>(type: "boolean", nullable: false, comment: "逻辑删除"),
                    create_id = table.Column<long>(type: "bigint", nullable: false, comment: "创建者Id"),
                    create_by = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false, comment: "创建者"),
                    create_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "创建时间"),
                    modify_id = table.Column<long>(type: "bigint", nullable: true, comment: "修改Id"),
                    modify_by = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: true, comment: "更新者"),
                    modify_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "修改日期")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_roles", x => x.id);
                },
                comment: "角色表");

            migrationBuilder.CreateTable(
                name: "user_roles",
                columns: table => new
                {
                    id = table.Column<long>(type: "bigint", nullable: false, comment: "泛型主键Tkey")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    user_id = table.Column<long>(type: "bigint", nullable: false, comment: "用户Id"),
                    role_id = table.Column<long>(type: "bigint", nullable: false, comment: "角色Id"),
                    is_deleted = table.Column<bool>(type: "boolean", nullable: false, comment: "逻辑删除"),
                    create_id = table.Column<long>(type: "bigint", nullable: false, comment: "创建者Id"),
                    create_by = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false, comment: "创建者"),
                    create_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "创建时间"),
                    modify_id = table.Column<long>(type: "bigint", nullable: true, comment: "修改Id"),
                    modify_by = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: true, comment: "更新者"),
                    modify_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "修改日期")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_user_roles", x => x.id);
                },
                comment: "用户跟角色关联表");

            migrationBuilder.CreateTable(
                name: "users",
                columns: table => new
                {
                    id = table.Column<long>(type: "bigint", nullable: false, comment: "泛型主键Tkey")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    login_name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false, comment: "登录账号"),
                    login_pwd = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false, comment: "登录密码"),
                    real_name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false, comment: "真实姓名"),
                    status = table.Column<int>(type: "integer", nullable: false, comment: "状态"),
                    remark = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true, comment: "备注"),
                    critical_modify_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "关键业务修改时间"),
                    last_error_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "最后异常时间"),
                    error_count = table.Column<int>(type: "integer", nullable: false, comment: "错误次数"),
                    gender = table.Column<int>(type: "integer", nullable: false, comment: "性别"),
                    birth_date = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "生日"),
                    address = table.Column<string>(type: "text", nullable: true, comment: "地址"),
                    enable = table.Column<bool>(type: "boolean", nullable: false, comment: "是否启用"),
                    is_deleted = table.Column<bool>(type: "boolean", nullable: false, comment: "逻辑删除"),
                    create_id = table.Column<long>(type: "bigint", nullable: false, comment: "创建者Id"),
                    create_by = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false, comment: "创建者"),
                    create_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "创建时间"),
                    modify_id = table.Column<long>(type: "bigint", nullable: true, comment: "修改Id"),
                    modify_by = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: true, comment: "更新者"),
                    modify_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "修改日期")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_users", x => x.id);
                },
                comment: "用户信息表");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "modules");

            migrationBuilder.DropTable(
                name: "permissions");

            migrationBuilder.DropTable(
                name: "role_module_permissions");

            migrationBuilder.DropTable(
                name: "roles");

            migrationBuilder.DropTable(
                name: "user_roles");

            migrationBuilder.DropTable(
                name: "users");
        }
    }
}
